using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEngine;
using UnityEngine.Networking;

public class XmlConfig : MonoBehaviour
{
    //PorijectSettings
    public int Width, Height, Left, Top, Topmost;
    public bool isFullScrean,isCursor, isWindowMinimize;
    public string Time;
    public float MaxTimeOffset;
    //UdpSetting
    public string SendIp, RecIp;
    public int SendPort, RecPort;
    //SerialProtSetting
    public string PortName;
    public int BaudRate, DataBits;

    private string filePath;
    private static XmlConfig _instance = null;
    public static XmlConfig GetInstance()
    {
        if (_instance == null)
        {
            _instance = new XmlConfig();
        }
        return _instance;
    }

    private void Awake()
    {
        _instance = this;
        ParseXmlTime();
        ParseXml();
    }


    private void Start()
    {


        if (Application.platform != RuntimePlatform.WindowsEditor)
        {
            //打包窗口设置
            if (isWindowMinimize)
            {
                U3DWindow.OnClickMinimize();
            }
            else
            {
                U3DWindow.SetWindowTopNew(Topmost, Left, Top, Width, Height, isFullScrean);
            }
            Cursor.visible = isCursor;
        }
        else
        {
            Debug.Log(Application.platform);
        }
    }

    void ParseXmlTime()
    {
        filePath = UnityWebRequestFile("Time.xml");


        //string filePath = Application.streamingAssetsPath + "/Time.xml";

        XmlDocument xmlDoc = new XmlDocument();

        //跳过BOM 
        System.IO.StringReader stringReader = new System.IO.StringReader(filePath);
        stringReader.Read();
        string result = stringReader.ReadToEnd();
        //关闭
        stringReader.Close();



        xmlDoc.LoadXml(result);
        XmlNodeList node = xmlDoc.SelectSingleNode("Database").ChildNodes;


        //遍历节点
        foreach (XmlElement ele in node)
        {
            //item下面的节点

            if (ele.Name == "ProjectSettings")
            {
                foreach (XmlElement i1 in ele.ChildNodes)
                {

                    switch (i1.Name)
                    {
                        case "Time":
                            Time = i1.InnerText.Trim();
                            break;
                    }

                }
            }
        }

    }
    //解析xml
    void ParseXml()
    {


        filePath = UnityWebRequestFile("Config.xml");

        //string filePath = Application.streamingAssetsPath + "/Time.xml";

        XmlDocument xmlDoc = new XmlDocument();

        //跳过BOM 
        System.IO.StringReader stringReader = new System.IO.StringReader(filePath);
        stringReader.Read();
        string result = stringReader.ReadToEnd();
        //关闭
        stringReader.Close();


        xmlDoc.LoadXml(result);
        XmlNodeList node = xmlDoc.SelectSingleNode("Database").ChildNodes;


        //遍历节点
        foreach (XmlElement ele in node)
        {
            //item下面的节点

            if (ele.Name == "ProjectSettings")
            {
                foreach (XmlElement i1 in ele.ChildNodes)
                {

                    switch (i1.Name)
                    {
                        case "Width":
                            Width = int.Parse(i1.InnerText);
                            break;
                        case "Height":
                            Height = int.Parse(i1.InnerText);
                            break;
                        case "Left":
                            Left = int.Parse(i1.InnerText);
                            break;
                        case "Top":
                            Top = int.Parse(i1.InnerText);
                            break;
                        case "Topmost":
                            string s = i1.InnerText;
                            if (s == "false" || s == "False")
                            {
                                Topmost = 1;
                            }
                            else
                            {
                                Topmost = -1;
                            }
                            break;
                        case "isFullScrean":
                            isFullScrean = Convert.ToBoolean(i1.InnerText);
                            break;
                        case "isCursor":
                            isCursor = Convert.ToBoolean(i1.InnerText);
                            break;
                        case "isWindowMinimize":
                            isWindowMinimize = Convert.ToBoolean(i1.InnerText);
                            break;
                        case "MaxTimeOffset":
                            MaxTimeOffset = float.Parse(i1.InnerText);
                            break;

                    }

                }
            }

            if (ele.Name == "UdpSetting")
            {
                foreach (XmlElement i1 in ele.ChildNodes)
                {

                    switch (i1.Name)
                    {
                        case "SendIp":
                            SendIp = i1.InnerText;
                            break;
                        case "SendPort":
                            SendPort = int.Parse(i1.InnerText);
                            break;
                        case "RecIp":
                            RecIp = i1.InnerText;
                            break;
                        case "RecPort":
                            RecPort = int.Parse(i1.InnerText);
                            break;
                    }
                }
            }

            if (ele.Name == "SerialProtSetting")
            {
                foreach (XmlElement i1 in ele.ChildNodes)
                {

                    switch (i1.Name)
                    {

                        case "PortName":
                            PortName = i1.InnerText.ToUpper();
                            break;
                        case "BaudRate":
                            BaudRate = int.Parse(i1.InnerText);
                            break;
                        case "DataBits":
                            DataBits = int.Parse(i1.InnerText);
                            break;
                    }
                }
            }

        }

    }


    /// <summary>
    /// 通过UnityWebRequest获取本地StreamingAssets文件夹中的文件
    /// </summary>
    /// <param name="fileName">文件名称</param>
    /// <returns></returns>
    public string UnityWebRequestFile(string fileName)
    {
        string url;
        #region 分平台判断 StreamingAssets 路径
            #if UNITY_EDITOR || UNITY_STANDALONE
                url = "file://" + Application.dataPath + "/StreamingAssets/" + fileName;
            #elif UNITY_IPHONE
                 url = "file://" + Application.dataPath + "/Raw/"+ fileName;
        #elif UNITY_ANDROID
                 url = "jar:file://" + Application.dataPath + "!/assets/"+ fileName;
        #endif
        #endregion


        UnityWebRequest request = UnityWebRequest.Get(url);
        request.SendWebRequest();//读取数据
        while (true)
        {
            if (request.downloadHandler.isDone)//是否读取完数据
            {
                return request.downloadHandler.text;
            }
        }

    }
    
    public string UnityAndroidFile(string fileName)
    {
        string url;
        #region 分平台判断 StreamingAssets 路径
#if UNITY_EDITOR || UNITY_STANDALONE
        url = "file://" + Application.dataPath + "/StreamingAssets/" + fileName;
#elif UNITY_IPHONE
                 url = "file://" + Application.dataPath + "/Raw/"+ fileName;
#elif UNITY_ANDROID
                 url = "jar:file://" + Application.dataPath + "!/assets/"+ fileName;
#endif
        #endregion


        UnityWebRequest request = UnityWebRequest.Get(url);
        request.SendWebRequest();//读取数据
        while (true)
        {
            if (request.downloadHandler.isDone)//是否读取完数据
            {
                return request.downloadHandler.text;
            }
        }

    }
}
